<?php

namespace app\stores\controller\admin\cash;

use app\BaseController;
use app\stores\model\cash\ThirdSettlementModel;

class SettlementManage extends BaseController{
    /**
     * 分类
     */
    public function category(){
        $category[] = [
            'settlement_type'=>1,
            'name'=>'团购',
            'num'=>ThirdSettlementModel::where(['settlement_type'=>1,'shop_id'=>$this->request->shopId,'is_delete'=>0])->count()
        ];
        $category[] = [
            'settlement_type'=>2,
            'name'=>'抵用券',
            'num'=>ThirdSettlementModel::where(['settlement_type'=>2,'shop_id'=>$this->request->shopId,'is_delete'=>0])->count()
        ];
        $category[] = [
            'settlement_type'=>3,
            'name'=>'外卖',
            'num'=>ThirdSettlementModel::where(['settlement_type'=>3,'shop_id'=>$this->request->shopId,'is_delete'=>0])->count()
        ];
        $category[] = [
            'settlement_type'=>4,
            'name'=>'收款',
            'num'=>ThirdSettlementModel::where(['settlement_type'=>4,'shop_id'=>$this->request->shopId,'is_delete'=>0])->count()
        ];
        return $this->success($category);
    }

    /**
     * 列表
     */
    public function list(){
        $status = $this->request->param('status/d',0);
        $search = $this->request->param('search');
        $settlement_type = $this->request->param('settlement_type/d',0);//1团购2抵用券3外卖4收款

        if(!in_array($settlement_type,[0,1,2,3,4])) return $this->error('结算方式分类有误');

        if($status>0) $where['status'] = $status;
        if($search) $where['search'] = $search;
        if($settlement_type>0) $where['settlement_type'] = $settlement_type;

        $where['shop_id'] = $this->request->shopId;

        $order = ['add_time'=>'desc'];
        $field = 'ts.*,a.number,a.username';

        $return = ThirdSettlementModel::list($where,$order,$this->request->page,$this->request->limit,$field);

        return $this->success($return);
    }
    /**
     * 添加/编辑结算方式
     */
    public function save()

    {
        $third_settlement_id = $this->request->param('third_settlement_id/d',0);
        $settlement_type = $this->request->param('settlement_type/d',0);//1团购2抵用券3外卖4收款
        $settlement_name = $this->request->param('settlement_name');
        $settlement_type_lable = $this->request->param('settlement_type_lable');
        $par_value = $this->request->param('par_value/f',0);
        $settlement_money = $this->request->param('settlement_money/f',0);
        $amount_money = $this->request->param('amount_money/f',0);
        $fee = $this->request->param('fee/f',0);
        $is_paid_in = $this->request->param('is_paid_in/d',1);
        $is_fee_paid_in = $this->request->param('is_fee_paid_in/d',1);
        $is_cash_payment = $this->request->param('is_cash_payment/d',1);
        $is_payment_integral = $this->request->param('is_payment_integral/d',0);
        $remarks = $this->request->param('remarks');
        $origin_type = $this->request->param('origin_type/d',1);

        if($third_settlement_id){
            $ts = ThirdSettlementModel::info(['third_settlement_id'=>$third_settlement_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]);
            if(empty($ts)) return $this->error('结算方式不存在');
        }

        if(!in_array($settlement_type,[1,2,3,4])) return $this->error('结算方式分类有误');
        if(empty($settlement_name)) return $this->error('结算方式名称不能为空');
        if($settlement_type==2 && $par_value<=0) return $this->error('面值不能小于等于0');
        if($settlement_type==2 && $amount_money<=0) return $this->error('实收金额不能小于等于0');
        if($settlement_type==1 && $settlement_money<=0) return $this->error('结算金额不能小于等于0');

        $data['settlement_type'] = $settlement_type;
        $data['settlement_name'] = $settlement_name;
        $data['settlement_type_lable'] = $settlement_type_lable;
        if($settlement_type==2) $data['par_value'] = $par_value;
        if($settlement_type==1) $data['settlement_money'] = $settlement_money;
        if($settlement_type==2) $data['amount_money'] = $amount_money;
        $data['fee'] = $fee;
        $data['is_paid_in'] = $is_paid_in;
        $data['is_fee_paid_in'] = $is_fee_paid_in;
        $data['is_cash_payment'] = $is_cash_payment;
        $data['is_payment_integral'] = $is_payment_integral;
        $data['remarks'] = $remarks;
        $data['add_ip'] = request()->ip();
        $data['shop_id'] = $this->request->shopId;
        $data['account_id'] = $this->request->accountId;
        $data['origin_type'] = $origin_type;

        if($third_settlement_id){
            ThirdSettlementModel::update($data,['third_settlement_id'=>$third_settlement_id]);
        }else{
            ThirdSettlementModel::insertGetId($data);
        }

        return $this->success('保存成功');

    }

    /**
     * 获取结算方式信息
     */
    public function info()

    {
        $third_settlement_id = $this->request->param('third_settlement_id/d',0);

        $ts = ThirdSettlementModel::info(['third_settlement_id'=>$third_settlement_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]);
        if(empty($ts)) return $this->error('结算方式不存在');

        return $this->success($ts);

    }
    /**
     * 状态
     */
    public function updateStatus()

    {
        $third_settlement_id = $this->request->param('third_settlement_id/d',0);
        $status = $this->request->param('status/d',1);

        $ts = ThirdSettlementModel::info(['third_settlement_id'=>$third_settlement_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]);
        if(empty($ts)) return $this->error('结算方式不存在');

        ThirdSettlementModel::update(['status'=>$status],['third_settlement_id'=>$third_settlement_id]);

        return $this->success('设置成功');

    }
    /**
     * 删除
     */
    public function del()

    {
        $third_settlement_ids = $this->request->param('third_settlement_id/[]',0);

        ThirdSettlementModel::whereIn('third_settlement_id',$third_settlement_ids)->where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->save(['is_delete'=>1]);

        return $this->success('删除成功');

    }

}

